package com.example.design.ddd.practice2.domain.order.valueobject;

import lombok.Getter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;

/**
 * 值对象 - 货币
 */
@Getter
public class Money {
    /**
     * 金额
     */
    private final BigDecimal amount;
    /**
     * 币种
     */
    private final Currency currency;

    public Money(BigDecimal amount, Currency currency) {
        this.amount = amount.setScale(2, RoundingMode.HALF_UP);
        this.currency = currency;
    }

    /**
     * 货币增加
     * @param other
     * @return
     */
    public Money add(Money other) {
        checkCurrency(other);
        return new Money(this.amount.add(other.amount), currency);
    }

    /**
     * 校验货币值对象
     * @param other
     */
    private void checkCurrency(Money other) {
        if (!this.currency.equals(other.currency)) {
            throw new IllegalArgumentException("不同币种不能相加");
        }
    }
}